/*

Copyright (c) 2005-2008, Simon Howard

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

 */

use std::ptr::null_mut;

const QUEUE_NULL: *mut () = null_mut();

struct QueueEntry {
    data: *mut (),
    prev: *mut QueueEntry,
    next: *mut QueueEntry,
}

struct Queue {
    head: *mut QueueEntry,
    tail: *mut QueueEntry,
}

impl Queue {
    fn new() -> *mut Queue {
        let mut queue = Box::new(Queue {
            head: null_mut(),
            tail: null_mut(),
        });

        Box::into_raw(queue)
    }

    fn free(queue: *mut Queue) {
        unsafe {
            while !Self::is_empty(queue) {
                Self::pop_head(queue);
            }
            Box::from_raw(queue);
        }
    }

    fn push_head(queue: *mut Queue, data: *mut ()) -> i32 {
        unsafe {
            let new_entry = Box::new(QueueEntry {
                data,
                prev: null_mut(),
                next: (*queue).head,
            });

            let new_entry_ptr = Box::into_raw(new_entry);

            if (*queue).head.is_null() {
                (*queue).head = new_entry_ptr;
                (*queue).tail = new_entry_ptr;
            } else {
                (*(*queue).head).prev = new_entry_ptr;
                (*queue).head = new_entry_ptr;
            }

            1
        }
    }

    fn pop_head(queue: *mut Queue) -> *mut () {
        unsafe {
            if Self::is_empty(queue) {
                return QUEUE_NULL;
            }

            let entry = (*queue).head;
            (*queue).head = (*entry).next;

            let result = (*entry).data;

            if (*queue).head.is_null() {
                (*queue).tail = null_mut();
            } else {
                (*(*queue).head).prev = null_mut();
            }

            Box::from_raw(entry);

            result
        }
    }

    fn peek_head(queue: *mut Queue) -> *mut () {
        unsafe {
            if Self::is_empty(queue) {
                QUEUE_NULL
            } else {
                (*(*queue).head).data
            }
        }
    }

    fn push_tail(queue: *mut Queue, data: *mut ()) -> i32 {
        unsafe {
            let new_entry = Box::new(QueueEntry {
                data,
                prev: (*queue).tail,
                next: null_mut(),
            });

            let new_entry_ptr = Box::into_raw(new_entry);

            if (*queue).tail.is_null() {
                (*queue).head = new_entry_ptr;
                (*queue).tail = new_entry_ptr;
            } else {
                (*(*queue).tail).next = new_entry_ptr;
                (*queue).tail = new_entry_ptr;
            }

            1
        }
    }

    fn pop_tail(queue: *mut Queue) -> *mut () {
        unsafe {
            if Self::is_empty(queue) {
                return QUEUE_NULL;
            }

            let entry = (*queue).tail;
            (*queue).tail = (*entry).prev;

            let result = (*entry).data;

            if (*queue).tail.is_null() {
                (*queue).head = null_mut();
            } else {
                (*(*queue).tail).next = null_mut();
            }

            Box::from_raw(entry);

            result
        }
    }

    fn peek_tail(queue: *mut Queue) -> *mut () {
        unsafe {
            if Self::is_empty(queue) {
                QUEUE_NULL
            } else {
                (*(*queue).tail).data
            }
        }
    }

    fn is_empty(queue: *mut Queue) -> bool {
        unsafe { (*queue).head.is_null() }
    }
}